import { _decorator, AssetManager, assetManager, Component, director, Node, ProgressBar, resources, Sprite, SpriteFrame } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('LoadingManager')
export class LoadingManager extends Component {

    @property(ProgressBar)
    private progressBar:ProgressBar;

    public static bigFrames = new Map<string, SpriteFrame>();

    private allBigs:string[] = ["fight_bgv2","game_root_bg", "home_bg", "weapon_bg"];

    private loadNum:number = 0;

    start() {
        this.schedule(() => {
            if (this.progressBar.progress < 1) {
                this.progressBar.progress+=0.1;
            } else {
                if (this.loadNum == this.allBigs.length) {
                    director.loadScene("home");
                }
            }
            
        }, 0.3, 200, 0.3);

        assetManager.loadBundle("big",(err, bundle:AssetManager.Bundle)=>{
            for (const b of this.allBigs) {
                const name = b + "/spriteFrame";
                    bundle.load(name, SpriteFrame, (err, spriteFrame: SpriteFrame) => {
                    console.log("spriteFrame:", spriteFrame);
                    LoadingManager.bigFrames.set(spriteFrame.name, spriteFrame);
                    this.loadNum++;
                    console.log("loading big bundle..." + spriteFrame.name);
                });
            }
        });
    }

    update(deltaTime: number) {}
}

